package com.nwu.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.nwu.entities.patentForm;
import com.nwu.entities.vo.PatentQuery;
import com.nwu.service.PatentManagementService;
import com.nwu.util.Result;
import com.nwu.util.resultCode.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  增删改查接口
 * </p>
 *
 * @author wangb
 * @since 2022-04-11
 * @since 2022-4-15
 */


@Api(tags = "专利管理")
@RestController
@RequestMapping("/ResearchProjectCollectionSpringBoot/getPatentInfo")
public class PatentManagementController {
    @Autowired
    private PatentManagementService patentManagementService;
    //查询所有信息


    @ApiOperation("查询所有专利信息")
    @GetMapping("findAll")
    public R findAllInfo(){
        List<patentForm> list = patentManagementService.list(null);
        return R.ok().data("items",list);
    }

    @ApiOperation("逻辑删除专利信息")
    @DeleteMapping("{id}")
    public R removePaper(@PathVariable Long id){
        boolean flag = patentManagementService.removeById(id);
        if(flag){
            return R.ok();
        }else{
            return R.error();
        }
    }

    //分页查询          current 当前页，limit记录数
    @ApiOperation("分页查询专利信息")
    @GetMapping("pageFind/{current}/{limit}")
    public R pageList(@PathVariable long current,
                           @PathVariable long limit){
        //创建page对象
        Page<patentForm> pageSciResearch =new Page<>(current,limit);
        patentManagementService.page(pageSciResearch,null);
        long total = pageSciResearch.getTotal();   //总记录数
        List<patentForm> records = pageSciResearch.getRecords();

        Map map =new HashMap();
        map.put("total",total);
        map.put("rows",records);
        return R.ok().data(map);
    }

    //条件查询带分页
    @ApiOperation("带条件的分页查询专利信息")
    @PostMapping("pageConditionFind/{current}/{limit}")
    public R pageConditionFind(@PathVariable long current, @PathVariable long limit,
                               @RequestBody(required = false)PatentQuery patentQuery){

        //创建page对象，传递当前页和每页记录数
        Page<patentForm> pageSciResearch =new Page<>(current,limit);
        QueryWrapper<patentForm> wrapper=new QueryWrapper<>();
        //wrapper构建条件,多条件组合查询 动态sql，判断条件是否为空，如果不为空拼接条件
        String patent_name = patentQuery.getPatent_name();
        Integer author_id = patentQuery.getAuthor_id();
        Integer patent_type = patentQuery.getPatent_type();
        String patent_app_time = patentQuery.getPatent_app_time();
        String patent_pub_time = patentQuery.getPatent_pub_time();
        String patent_aut_time = patentQuery.getPatent_aut_time();

        if(!StringUtils.isEmpty(patent_name)){
            wrapper.like("patent_name",patent_name);
        }
        if(!StringUtils.isEmpty(author_id)){
            wrapper.eq("author_id",author_id);
        }
        if(!StringUtils.isEmpty(patent_type)){
            wrapper.eq("patent_type",patent_type);
        }
        if(!StringUtils.isEmpty(patent_app_time)){
            wrapper.eq("patent_app_time",patent_app_time);
        }
        if(!StringUtils.isEmpty(patent_pub_time)){
            wrapper.eq("patent_pub_time",patent_pub_time);
        }
        if(!StringUtils.isEmpty(patent_aut_time)){
            wrapper.eq("patent_aut_time",patent_aut_time);
        }

        patentManagementService.page(pageSciResearch,wrapper);
        long total = pageSciResearch.getTotal();   //总记录数
        List<patentForm> records = pageSciResearch.getRecords(); //数据list集合

        return R.ok().data("total",total).data("rows",records);
    }

    //教师添加专利信息
    @ApiOperation("添加专利信息")
    @PostMapping("addPatentManagement")
    public R addPatentManagement(@RequestBody patentForm patent){
        patent.setIsDeleted(0);
        boolean save = patentManagementService.save(patent);
        if(save){
            return R.ok();
        }else{
            return R.error();
        }

    }

    @ApiOperation("根据专利id查询")
    @GetMapping("GetPatent/{patent_id}")
    public String getThesis(@PathVariable String patent_id){
          patentForm paper = patentManagementService.getById(patent_id);
          return Result.ok("paper",paper);

      }

    //专利信息修改
    @ApiOperation("专利信息修改")
    @PostMapping("updatePatent")
    public R updateThesis(@RequestBody patentForm patent){
        boolean flag = patentManagementService.updateById(patent);
        if(flag){
            return R.ok();
        }else{
            return R.error();
        }
    }

    }

